package cn.tedu.weibo1.controller;

import cn.tedu.weibo1.mapper.CommentMapper;
import cn.tedu.weibo1.mapper.WeiboMapper;
import cn.tedu.weibo1.pojo.dto.WeiboDto;
import cn.tedu.weibo1.pojo.entity.Weibo;
import cn.tedu.weibo1.pojo.vo.ResultVo;
import cn.tedu.weibo1.pojo.vo.UserVo;
import cn.tedu.weibo1.pojo.vo.WeiboDetailVo;
import cn.tedu.weibo1.pojo.vo.WeiboIndexVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/weibo/")
public class WeiboController {
    @Autowired
    WeiboMapper mapper;
    @RequestMapping("insert")
    public int insert(@RequestBody WeiboDto weiboDto, HttpSession session) {
        UserVo user = (UserVo) session.getAttribute("user");
        if(user == null) {
            return 2;
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDto,weibo);
        weibo.setUserId(user.getId());
        weibo.setCreated(new Date());
        mapper.insert(weibo);
        return 1;
    }
    @RequestMapping("select")
    public List<WeiboIndexVo> select() {
        return mapper.select();
    }
    @RequestMapping("selectById")
    public WeiboDetailVo selectById(int id) {
        return mapper.selectById(id);
    }
    @RequestMapping("select/mine")
    public ResultVo selectMine(HttpSession session){
        UserVo user = (UserVo) session.getAttribute("user");
        if(user == null) {
            return new ResultVo(2, "用户未登录", null);
        }
        return new ResultVo(1,"请求成功",mapper.selectMine(user.getId()));
    }
    @Autowired
    CommentMapper commentMapper;
    @RequestMapping("delete")
    public void delete(int id){
        commentMapper.deleteByWeiboId(id);
        mapper.deleteById(id);

    }
}
